package com.stu;

/**
 * @author Xue ShuWen
 * @date 2023/6/19 10:45
 */

import com.google.gson.Gson;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;
import com.bean.*;

public  class StuData {
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String    url = "jdbc:mysql://localhost:3306/stuInfo";
    static String   root = "root";
    static String     ps = "root";

    Connection coon = null;
    Statement st = null;
    ResultSet rs = null;


    public String queryStu(String str) throws ClassNotFoundException {
        Class.forName(driver);
        String sql = "SELECT * FROM studentInfo where str ='"+str+"';";

        try {
            coon = DriverManager.getConnection(url,root,ps);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ArrayList<StuList> stulists = new ArrayList<>();
        try {
            st = coon.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()){
                StuList stuList = new StuList();
                stuList.setClassNo(rs.getInt(1));
                stuList.setStr(rs.getString(2));
                stuList.setGender(rs.getInt(3));
                stuList.setB(rs.getString(4));
                stuList.setStudId(rs.getInt(5));
                stuList.setSemster(rs.getInt(6));
                stuList.setS(rs.getString(7));
                stuList.setGPA(rs.getDouble(8));
                stulists.add(stuList);
                System.out.println(stuList);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(new Gson().toJson(stulists));
        return "{\"data\":"+new Gson().toJson(stulists)+"}";

    }
    public static void main(String[]args) throws ClassNotFoundException, SQLException {
        Scanner input = new Scanner(System.in);

        System.out.println("请输入账号:");
        String str = input.next();
        new StuData().queryStu(str);

    }
}













